package com.goktech.project.hmw.controller;

import com.goktech.project.hmw.mapper.CountyProject;
import com.goktech.project.hmw.service.ICountyProjectService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/county")
public class CountyProjectController {
	@Autowired
	private StringRedisTemplate redisTemplate;

	//	@Autowired
//	private RedisTemplate template;
	@Autowired
	private ICountyProjectService countyProjectService;
	/**
	 * 区县总体人数金额
	 * @return
	 */

	@PostMapping("/listtotal")
	public List<CountyProject> listTotal(){
//		List<CountyProject> list=null;
//		String county="listtotal11";
//		if (redisTemplate.hasKey(county)){
//			ValueOperations<String, String> operations = redisTemplate.opsForValue();
////			redisTemplate.delete(county);
//			String o= operations.get(county,0,-1);
//			JSONArray jsonArray = JSONArray.fromObject(o);
//			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
//		}else{
//			list=countyProjectService.listTotal();
//			ValueOperations<String, String> operations = redisTemplate.opsForValue();
//			String json = JSONArray.fromObject(list).toString();
//			operations.set(county,json);
//
//		}
		return countyProjectService.listTotal();
	}

	/**
	 * 区县按项目人数金额
	 * @return
	 */
	@PostMapping("/listproject")
	public List<CountyProject> listProjectPersonCaption(){
		return countyProjectService.listProjectPersonCaption();
	}

	/**
	 * 区县按时间人数金额
	 * @return
	 */
	@PostMapping("/listyear")
	public List<CountyProject> listYearPersonCaption(){
		return countyProjectService.listYearPersonCaption();
	}

	/**
	 * 区县人数项目人均发放
	 * @return
	 */
	@PostMapping("/listavl")
	public List<CountyProject> listAvlProjectPersonCaption(){
		return countyProjectService.listAvlProjectPersonCaption();
	}

	/**
	 * 前十
	 * @return
	 */
	@PostMapping("/listtop")
	public List<CountyProject> listTop10(@RequestBody Map<String,String> reqMap){
		return countyProjectService.listTop10(reqMap.get("year"));
	}

	/**
	 * 福州市总金额人数逐年统计
	 * @return
	 */
	@PostMapping("/listcitytotal")
	public List<CountyProject> listCityTotalCapitalPerson(){
		return countyProjectService.listCityTotalCapitalPerson();
	}

	/**
	 * 通过区县名称获取区县数据按照时间纬度
	 * @return
	 */
	@PostMapping("/listpersoncapital")
	public List<CountyProject> listPersonCapitalYearByCounty(@RequestBody Map<String,String> reqMap){
		return countyProjectService.listPersonCapitalYearByCounty(reqMap.get("county"));
	}

	/**
	 * 通过区县获取区县人数项目人均发放 前top
	 * @param reqMap
	 * @return
	 */
	@PostMapping("listavltop5")
	public List<CountyProject> listAvlProjectPersonCaptionByCounty(@RequestBody Map<String,String> reqMap){
		//return countyProjectService.listAvlProjectPersonCaptionByCounty(reqMap.get("county"),reqMap.get("top"));
		return countyProjectService.listAvlProjectPersonCaptionTopByCounty(reqMap.get("county"),reqMap.get("top"));
	}

	/**
	 * 通过城市、区县获取下一级数据
	 * @param reqMap
	 * @return
	 */
	@PostMapping("/listtownship")
	public List<CountyProject> listTownshipTotal(@RequestBody Map<String,String> reqMap){
		if(!reqMap.get("city").isEmpty()){
			return countyProjectService.listTotal();
		}else {
			return countyProjectService.listTownshipTotal(reqMap.get("county"));
		}
	}

	/**
	 * 区县按项目人数金额TOP
	 * @param reqMap
	 * @return
	 */
	@PostMapping("/listprocaptop")
	public List<CountyProject> listProjectPersonCaptionTop(@RequestBody Map<String,String> reqMap) {
		return countyProjectService.listProjectPersonCaptionTop(reqMap.get("column"),reqMap.get("top"));
	}

	/**
	 * 按项目地区获取前top
	 * @param reqMap
	 * @return
	 */
	@PostMapping("/listright2")
	public List<CountyProject> listRight2(@RequestBody Map<String,String> reqMap){
		return countyProjectService.listRight2(reqMap.get("type"),reqMap.get("top"));
	}

	/**
	 * 右1
	 * @param reqMap
	 * @return
	 */
	@PostMapping("/listright1")
	public  List<CountyProject> listRight1(@RequestBody Map<String,String> reqMap){
		return countyProjectService.listRight1(reqMap.get("type"),reqMap.get("top"));
	}

	/**
	 * 获取项目的总金额总人数项目参与的总区县()
	 * @return
	 */
	@PostMapping("/allListProject")
	public  List<CountyProject> allListProject(){
//		List<List<CountyProject>> listAll=null;
//		List<CountyProject> list=null;
//		String county="allListProject";
//		if (redisTemplate.hasKey(county)){
//			ValueOperations<String, String> operations = redisTemplate.opsForValue();
////			redisTemplate.delete(county);
//			String o= operations.get(county,0,-1);
//			JSONArray jsonArray = JSONArray.fromObject(o);
//			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
//		}else{
//			list=countyProjectService.allListProject();
//			ValueOperations<String, String> operations = redisTemplate.opsForValue();
//			String json = JSONArray.fromObject(list).toString();
//			operations.set(county,json);
//
//		}
//		listAll.add(list);
//		listAll.add(countyProjectService.allproject());
		return countyProjectService.allListProject();
	}

	/**
	 *根据地区获取项目人均总发放金额总人数 项目地区镇乡总个数（点击右一）
	 * @return
	 */
	@PostMapping("/allListCounty")
	public  List<CountyProject> allListCounty(@RequestBody Map<String,String> reqMap){
//		List<CountyProject> list=null;
//		String county="";
//		if(reqMap.get("type")!=null && reqMap.get("type")!=""){
//			county+="allListCounty"+reqMap.get("county")+reqMap.get("type");
//		}else{
//			county+="allListCounty"+reqMap.get("county");
//		}
//		if (redisTemplate.hasKey(county)){
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=operations.get(county,0,-1);
//			JSONArray jsonArray = JSONArray.fromObject(json);
//			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
//		}else{
//			list=countyProjectService.allListCounty(reqMap);
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=JSONArray.fromObject(list).toString();
//			operations.set(county,json);
//		}
		return countyProjectService.allListCounty(reqMap);
	}

	/**
	 *所有区县人均发放金额（当前年）
	 * @return
	 */
	@PostMapping("/avlAllList")
	public  List<CountyProject> avlAllListCounty(){
//		List<CountyProject> list=null;
//		String county="avlAllList";
//		if (redisTemplate.hasKey(county)){
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=operations.get(county,0,-1);
//			JSONArray jsonArray = JSONArray.fromObject(json);
//			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
//		}else{
//			list=countyProjectService.avlAllListCounty();
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=JSONArray.fromObject(list).toString();
//			operations.set(county,json);
//		}
		return countyProjectService.avlAllListCounty();
	}

	/**
	 * 根据项目名获取项目人均总发放金额总人数 项目地区镇乡总个数（点击右一）
	 * @param reqMap
	 * @return
	 */
	@PostMapping("/pitchListProject")
	public  List<CountyProject> projectList(@RequestBody Map<String,String> reqMap){
//		List<CountyProject> list=null;
//		String project="";
//		if(reqMap.get("project")!=null && reqMap.get("project")!=""){
//			project+="pitchListProject"+reqMap.get("project")+reqMap.get("type");
//		}else{
//			project+="pitchListProject"+reqMap.get("project");
//		}
//		if (redisTemplate.hasKey(project)){
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=operations.get(project,0,-1);
//			JSONArray jsonArray = JSONArray.fromObject(json);
//			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
//		}else{
//			list=countyProjectService.pitchListProject(reqMap);
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=JSONArray.fromObject(list).toString();
//			operations.set(project,json);
//		}
		return countyProjectService.pitchListProject(reqMap);
	}

	/**
	 * 左二开始加载 查询全市发放总金额总人数
	 * @param reqMap
	 * @return
	 */
	@PostMapping("/yearList")
	public  List<CountyProject> yearList(@RequestBody Map<String,String> reqMap){
		List<CountyProject> list=null;
		String county="yearList"+reqMap.get("top");
		if (redisTemplate.hasKey(county)){
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=operations.get(county,0,-1);
			JSONArray jsonArray = JSONArray.fromObject(json);
			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
		}else{
			list=countyProjectService.yearList(reqMap.get("top"));
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=JSONArray.fromObject(list).toString();
			operations.set(county,json);
		}
		return list;

	}

	/**
	 * 右一点击区县 查询街道数 总金额总人数
	 * @param reqMap
	 * @return
	 */
	@PostMapping("/townshipList")
	public  List<CountyProject> townshipList(@RequestBody Map<String,String> reqMap){
//		List<CountyProject> list=null;
//		String county="townshipList"+reqMap.get("county");
//		if (redisTemplate.hasKey(county)){
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=operations.get(county,0,-1);
//			JSONArray jsonArray = JSONArray.fromObject(json);
//			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
//		}else{
//			list=countyProjectService.townshipList(reqMap.get("county"));
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=JSONArray.fromObject(list).toString();
//			operations.set(county,json);
//		}
		return countyProjectService.townshipList(reqMap);
	}

	/**
	 * 左三 点击区县 查询项目总金额总人数人均发放金额
	 * @param reqMap
	 * @return
	 */
	@PostMapping("/topCountyList")
	public  List<CountyProject> topCountyList(@RequestBody Map<String,String> reqMap){
//		List<CountyProject> list=null;
//		String county="topCountyList"+reqMap.get("county")+reqMap.get("top");
//		if (redisTemplate.hasKey(county)){
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=operations.get(county,0,-1);
//			JSONArray jsonArray = JSONArray.fromObject(json);
//			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
//		}else{
//			list=countyProjectService.topCountyList(reqMap.get("county"),reqMap.get("top"));
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=JSONArray.fromObject(list).toString();
//			operations.set(county,json);
//		}
		return countyProjectService.topCountyList(reqMap);

	}

	/**
	 * 左三 点击项目 查询地区总金额总人数人均发放金额
	 * @param reqMap
	 * @return
	 */
	@PostMapping("/topProjectList")
	public  List<CountyProject> topProjectList(@RequestBody Map<String,String> reqMap){
//		List<CountyProject> list=null;
//		String project="topProjectList"+reqMap.get("project")+reqMap.get("top");
//		if (redisTemplate.hasKey(project)){
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=operations.get(project,0,-1);
//			JSONArray jsonArray = JSONArray.fromObject(json);
//			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
//		}else{
//			list=countyProjectService.toppitchListProject(reqMap.get("project"),reqMap.get("top"));
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=JSONArray.fromObject(list).toString();
//			operations.set(project,json);
//		}
		return countyProjectService.toppitchListProject(reqMap);
	}

	/**
	 * 左二 点击项目 查询年度 项目总金额总人数
	 * @param reqMap
	 * @return
	 */
	@PostMapping("/topYearList")
	public  List<CountyProject> topYearList(@RequestBody Map<String,String> reqMap){
		List<CountyProject> list=null;
		String project="topYearList"+reqMap.get("project")+reqMap.get("top");
		if (redisTemplate.hasKey(project)){
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=operations.get(project,0,-1);
			JSONArray jsonArray = JSONArray.fromObject(json);
			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
		}else{
			list=countyProjectService.topYearList(reqMap.get("project"),reqMap.get("top"));
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=JSONArray.fromObject(list).toString();
			operations.set(project,json);
		}
		return list;
	}

	/**
	 *左一 点击项目 查询 地区总金额总人数
	 * @param reqMap
	 * @return
	 */
	@PostMapping("/topTotalProjectList")
	public  List<CountyProject> topTotalProjectList(@RequestBody Map<String,String> reqMap){
//		List<CountyProject> list=null;
//		String project="topTotalProjectList"+reqMap.get("project");
//		if (redisTemplate.hasKey(project)){
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=operations.get(project,0,-1);
//			JSONArray jsonArray = JSONArray.fromObject(json);
//			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
//		}else{
//			list= countyProjectService.topTotalProjectList(reqMap.get("project"));
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=JSONArray.fromObject(list).toString();
//			operations.set(project,json);
//		}
		return countyProjectService.topTotalProjectList(reqMap);
	}

	/**
	 *左二 点击地区 查询 项目总金额总人数
	 * @param reqMap
	 * @return
	 */
	@PostMapping("/topTotalCountyList")
	public  List<CountyProject> topTotalCountyList(@RequestBody Map<String,String> reqMap){
		List<CountyProject> list=null;
		String county="topTotalCountyList"+reqMap.get("county")+reqMap.get("top");
		if (redisTemplate.hasKey(county)){
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=operations.get(county,0,-1);
			JSONArray jsonArray = JSONArray.fromObject(json);
			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
		}else{
			list= countyProjectService.topTotalCountyList(reqMap.get("county"),reqMap.get("top"));
			ValueOperations<String,String> operations=redisTemplate.opsForValue();
			String json=JSONArray.fromObject(list).toString();
			operations.set(county,json);
		}
		return list;
	}

	/**
	 * 右一左一左三三级 在有项目地区这条件，查街道总人数总allListProject金额
	 * @param reqMap
	 * @return
	 */
	@PostMapping("/totalTownshipList")
	public  List<CountyProject> totalTownshipList(@RequestBody Map<String,String> reqMap){
		return countyProjectService.totalTownshipList(reqMap.get("county"),reqMap.get("project"));
	}

	/**
	 * 地区增幅 默认加载 左二（变动）
	 * @param reqMap
     * @return
     */
	@PostMapping("/indexAmplification")
	public  List<CountyProject> indexAmplification(@RequestBody Map<String,String> reqMap){
//		List<CountyProject> list=null;
//		String key="indexAmplification"+reqMap.get("year");
//		if (redisTemplate.hasKey(key)){
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=operations.get(key,0,-1);
//			JSONArray jsonArray = JSONArray.fromObject(json);
//			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
//		}else{
//			list= countyProjectService.indexAmplification(reqMap.get("year"));
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=JSONArray.fromObject(list).toString();
//			operations.set(key,json);
//		}
		return countyProjectService.indexAmplification(reqMap);
	}

	/**
	 * 地区增幅 点击地区加载 左二（变动）
	 * @param reqMap
     * @return
     */
	@PostMapping("/amplificationCounty")
	public  List<CountyProject> amplificationCounty(@RequestBody Map<String,String> reqMap){

//		List<CountyProject> list=null;
//		String key="amplificationCounty"+reqMap.get("year")+reqMap.get("county");
//		if (redisTemplate.hasKey(key)){
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=operations.get(key,0,-1);
//			JSONArray jsonArray = JSONArray.fromObject(json);
//			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
//		}else{
//			list= countyProjectService.amplificationCounty(reqMap.get("year"),reqMap.get("county"));
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=JSONArray.fromObject(list).toString();
//			operations.set(key,json);
//		}
		return countyProjectService.amplificationCounty(reqMap);
	}

	/**
	 * 地区增幅 点击项目加载 左二（变动）
	 * @param reqMap
	 * @return
	 */
	@PostMapping("/amplificationProject")
	public  List<CountyProject> amplificationProject(@RequestBody Map<String,String> reqMap){
//		List<CountyProject> list=null;
//		String key="amplificationProject"+reqMap.get("year")+reqMap.get("project");
//		if (redisTemplate.hasKey(key)){
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=operations.get(key,0,-1);
//			JSONArray jsonArray = JSONArray.fromObject(json);
//			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
//		}else{
//			list= countyProjectService.amplificationProject(reqMap.get("year"),reqMap.get("project"));
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=JSONArray.fromObject(list).toString();
//			operations.set(key,json);
//		}
		return countyProjectService.amplificationProject(reqMap);
	}

	@PostMapping("/townMap")
	public  List<CountyProject> townMap(@RequestBody Map<String,String> reqMap){
//		List<CountyProject> list=null;
//		String key="townMap"+reqMap.get("project");
//		if(!StringUtils.isEmpty(reqMap.get("county"))){
//			key+=reqMap.get("county");
//		}
//		//String key="amplificationProject"+reqMap.get("year")+reqMap.get("project");
//		if (redisTemplate.hasKey(key)){
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=operations.get(key,0,-1);
//			JSONArray jsonArray = JSONArray.fromObject(json);
//			list = (List<CountyProject>) JSONArray.toCollection(jsonArray,CountyProject.class);
//		}else{
//			list= countyProjectService.townMap(reqMap);
//			ValueOperations<String,String> operations=redisTemplate.opsForValue();
//			String json=JSONArray.fromObject(list).toString();
//			operations.set(key,json);
//		}
		return countyProjectService.townMap(reqMap);
	}

}

